{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python怎样自动发送邮件？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import smtplib\n",
    "from email.mime.text import MIMEText\n",
    "from email.header import Header"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 邮件内容\n",
    "mail_msg = \"\"\"\n",
    "    <h2 style=\"color:#f00\">这是一封自动发送的邮件</h2>\n",
    "    <p><a href=\"http://www.baidu.com\">点击这里有好玩的哈哈哈哈</a></p>\n",
    "\"\"\"\n",
    "message = MIMEText(mail_msg, 'html', 'utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 发件人名字，可以自由填写\n",
    "message['From'] = Header(\"蚂蚁学Python\", 'utf-8')\n",
    "# 收件人名字，可以自由填写\n",
    "message['To'] =  Header(\"葫芦娃大战蝙蝠侠\", 'utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 邮件标题\n",
    "subject = 'Python自动发来的邮件'\n",
    "message['Subject'] = Header(subject, 'utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 发送方\n",
    "sender = '693609645@qq.com'\n",
    "# 接收方，可以多个\n",
    "receivers = ['2326578190@qq.com']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "邮件发送成功\n"
     ]
    }
   ],
   "source": [
    "# 使用QQ邮箱的服务，发送邮件\n",
    "smtpObj = smtplib.SMTP_SSL(\"smtp.qq.com\", 465) \n",
    "smtpObj.login(sender, \"dkikfalymrgabfbd\")  \n",
    "smtpObj.sendmail(sender, receivers, message.as_string())\n",
    "smtpObj.quit()\n",
    "print('邮件发送成功')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
